package juc;

import java.util.concurrent.CyclicBarrier;


/**
 * @author 乔巴
 * @date 2021/7/29 10:20
 **/
public class CyclicBarrierTest {

    private static String appointmentPlace = "峡谷森林";
    static ThreadGroup  group1 = new ThreadGroup("大乔");
    static ThreadGroup  group2 = new ThreadGroup("铠");
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(2, () -> System.out.println("🌹🌹🌹到达约会地点：大乔和铠都来到了👉" + appointmentPlace));
        Thread 大乔 = new Thread(group1, () -> {
            say("铠，你在哪里...");
            try {
                cyclicBarrier.await(); //到达幽会地点
                say("铠，你终于来了...");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        Thread 铠 = new Thread(group2, () -> {
            try {
                Thread.sleep(500); //铠打野中
                say("我打个野，马上就到!");
                cyclicBarrier.await(); //到达幽会地点
                say("乔，不好意思，刚打野遇上兰陵王了，你还好吗？！");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        大乔.start();
        铠.start();
    }

    private static void say(String s) {
        System.out.println(s);
    }


}
